package com.area.cth.service.impl;

import com.area.cth.mapper.ExchangeMapper;
import com.area.cth.pojo.Exchange;
import com.area.cth.service.ExchangeService;
import com.area.cth.utils.ThreadLocalUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class ExchangeServiceImpl implements ExchangeService {
    @Autowired
    private ExchangeMapper em;

    @Override
    public void addExchange(Exchange e) {
        if (e.getIsFree().equals(1)) {
            e.setIntentionCategory(null);
        }
        e.setCreateTime(LocalDateTime.now());
        e.setUpdateTime(LocalDateTime.now());
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        e.setCreateUser(id);
        e.setAuditState("审核中");
        em.addExchange(e);
    }

    @Override
    public List<Exchange> getExchangeList() {
        return em.getExchangeListByIsFree(0);
    }

    @Override
    public List<Exchange> getFreeList() {
        return em.getExchangeListByIsFree(1);
    }

    @Override
    public List<Exchange> getMyExchangeTrueList() {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        String s = "已通过";
        return em.getMyExchangeListByAudit(id, s);
    }

    @Override
    public List<Exchange> getMyExchangeAuditList() {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        String s = "审核中";
        return em.getMyExchangeListByAudit(id, s);
    }

    @Override
    public List<Exchange> getMyExchangeFalseList() {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        String s = "未通过";
        return em.getMyExchangeListByAudit(id, s);
    }

    @Override
    public List<Exchange> getExchangeTrueByUserList(Integer userId) {
        String s = "已通过";
        return em.getExchangeTrueByUserList(userId, s);
    }

    @Override
    public Exchange getExchangeById(Integer id) {
        return em.getExchangeById(id);
    }

    @Override
    public void update(Exchange e) {
        e.setAuditState("审核中");
        em.update(e);
    }

    @Override
    public void delete(Integer id) {
        em.delete(id);
    }

    @Override
    public void updateAudit(String audit, Integer id) {
        em.updateAudit(audit, id);
    }

    @Override
    public IPage<Exchange> exchangeList(Integer pageNo, Integer pageSize, String auditState, Integer isFree, String keyword) {
        Page<Exchange> page = Page.of(pageNo, pageSize);
        IPage<Exchange> pb = em.exchangeList(page,auditState,isFree,keyword);
        return pb;
    }
}
